﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Model;
using System.Data;
using System.Data.SqlClient;
namespace Dal
{
    public class StuDal
    {
        string constrings = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;

        public int Add(Student stu)
        {
            using (SqlConnection con = new SqlConnection(constrings))
            {
                con.Open();

                string sql = "insert into Stu values('" + stu.ID + "','" + stu.Name + "','" + stu.Sex + "','" + stu.Age + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                return cmd.ExecuteNonQuery();
            }

        }


        public List<Student> Show()
        {
            List<Student> studentList = new List<Student>();

            using (SqlConnection con = new SqlConnection(constrings))
            {
                con.Open();
                string sql = "select * from Stu";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter dap = new SqlDataAdapter(cmd);

                DataTable dt = new DataTable();
                dap.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    Student student = new Student();
                    student.ID = Convert.ToInt32(dr["ID"]);
                    student.Name = dr["Name"].ToString();
                    student.Sex = dr["Sex"].ToString();
                    student.Age = Convert.ToInt32(dr["Age"]);
                    studentList.Add(student);
                }
                return studentList;



            }


        }
    }
}
